<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once APPPATH . 'controllers/admin.php';

/**
 * Контроллер изменения шаблонов писем
 *
 * @package CoreFramework
 * @version 1.0
 * @author Gennadiy Kozlenko
 */
class Admin_Edit_Emails extends Admin {
   
   /**
    * Массив шаблонов, исключаемых из редактирования
    *
    * @access private
    * @var array
    */
   var $_excludes = array();
   
   /**
    * Конструктор класса
    *
    * @access public
    * @return Admin_Edit_Emails
    */
   function Admin_Edit_Emails() {
      parent::Admin();
      $this->set_menu(2, 6);
   } // end Admin_Edit_Emails
   
   /**
    * Метод по умолчанию
    *
    * @access public
    */
   function index() {
      $data = array();
      $this->load->helper('file');
      // Получаем список лайаутов
      $emails = array();
      foreach (get_filenames(APPPATH . 'views/' . $this->config->item('language') . '/emails') as $file) {
         $name = preg_replace('~\.html$~', '', $file);
         if (!in_array($name, $this->_excludes)) {
            $emails[$name] = str_replace('{%email%}', ucfirst($name), $this->lang->line('email_template_item'));
         }
      }
      $email = '';
      $subject = '';
      $content = '';
      if (false !== $this->input->post('email')) {
         $email = basename(trim($this->input->post('email')));
      } elseif (false !== $this->input->get('email')) {
         $email = basename(trim($this->input->get('email')));
      } else {
         $email = current(array_keys($emails));
      }
      if (false !== $this->input->post('edit')) {
         $subject = $this->input->post('subject');
         $content = $this->input->post('content');
         // Сохраняем шаблон
         if (save_email($email, $subject, $content)) {
            $this->set_message($this->lang->line('edit_email_succ'), 'info', true);
            $this->redirect($this->get_controller(), 'index', array('email' => $email));
         }
         $this->set_message($this->lang->line('edit_email_error'), 'error');
      } else {
         // Получаем шаблон
         $content = read_file(APPPATH . 'views/' . $this->config->item('language') . '/emails/' . $email . '.html');
         $match = array();
         if (preg_match('~{%subject%}(.*?){%/subject%}~', $content, $match)) {
            $subject = $match[1];
            $content = ltrim(str_replace($match[0], '', $content));
         }
      }
      // Забиваем в дату поля
      $this->load->helper('element');
      $data['emails'] = create_select('email', $emails, $email, 'class="i" onchange="this.form.submit()"');
      // Защищаем специальные теги
      $subject = str_replace(array('{%', '%}'), array('{-%', '%-}'), $subject);
      $content = str_replace(array('{%', '%}'), array('{-%', '%-}'), $content);
      $data['subject'] = htmlspecialchars($subject);
      $data['content'] = htmlspecialchars($content);
      // Выводим
      $this->set_content($this->parser->parse('admin/edit_emails.html', $data, true));
      $this->display();
   } // end index
   
}
